home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / OWLINC.PAK / DOCTPL.H < prev    next >
C/C++ Source or Header  |  1997-05-06  |  13KB  |  408 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // Copyright (c) 1993, 1997 by Borland International, All Rights Reserved
  4. //
  5. //$Revision:   10.7  $
  6. //
  7. // Definition of class TDocTemplate, and class template TDocTemplateT<D,V>
  8. //----------------------------------------------------------------------------
  9. #if !defined(OWL_DOCTPL_H)
  10. #define OWL_DOCTPL_H
  11.  
  12. #if !defined(OWL_DEFS_H)
  13. # include <owl/defs.h>
  14. #endif
  15. #if !defined(WINSYS_REGISTRY_H)
  16. # include <winsys/registry.h>
  17. #endif
  18. #if !defined(CLASSLIB_OBJSTRM_H)
  19. # include <classlib/objstrm.h>
  20. #endif
  21.  
  22. #if defined(BI_NAMESPACE)
  23. namespace OWL {
  24. #endif
  25.  
  26. #if defined(_OWLDLL) && defined(BI_APP_DLL) && !defined(BI_PLAT_WIN32)
  27. # define _DOCVIEWENTRY __export   // templates in a DLL using DLLs
  28. #else
  29. # define _DOCVIEWENTRY
  30. #endif
  31.  
  32. class _OWLCLASS TDocTemplate;
  33. class _OWLCLASS TDocManager;
  34. class _OWLCLASS TModule;
  35. class _OWLCLASS TDocument;
  36. class _OWLCLASS TView;
  37.  
  38. //
  39. // Templates constructed before app get linked onto this head pointer
  40. //
  41. extern TDocTemplate* DocTemplateStaticHead;
  42.  
  43. // Generic definitions/compiler options (eg. alignment) preceeding the 
  44. // definition of classes
  45. #include <services/preclass.h>
  46.  
  47. //
  48. // class TDocTemplate
  49. // ~~~~~ ~~~~~~~~~~~~
  50. class _OWLCLASS TDocTemplate : public TRegLink, public TStreamableBase {
  51.   public:
  52.     virtual TDocument*      ConstructDoc(TDocument* parent = 0) = 0;
  53.     virtual TView*          ConstructView(TDocument& doc) = 0;
  54.     virtual TDocument*      IsMyKindOfDoc(TDocument& doc) = 0;
  55.     virtual TView*          IsMyKindOfView(TView& view) = 0;
  56.     virtual const char far* GetViewName() = 0;
  57.  
  58.     bool                    IsVisible(); // displayable in file select dialogs
  59.     virtual TDocTemplate*   Clone(TModule* module,
  60.                                   TDocTemplate*& phead=DocTemplateStaticHead)=0;
  61.  
  62.     TDocManager*            GetDocManager() const;
  63.     void                    SetDocManager(TDocManager* dm);
  64.     const char far*         GetDirectory() const;
  65.     void                    SetDirectory(const char far*);
  66.     void                    SetDirectory(const char far*, int len);
  67.     TDocTemplate*           GetNextTemplate() const;
  68.     bool                    IsFlagSet(long flag) const;
  69.     long                    GetFlags() const;
  70.     void                    SetFlag(long flag);
  71.     void                    ClearFlag(long flag);
  72.     bool                    IsStatic() const;
  73.     TModule*&               GetModule();
  74.     void                    SetModule(TModule* module);
  75.     int                     GetRefCount() const;
  76.  
  77.     const char far*         GetFileFilter() const;
  78.     const char far*         GetDescription() const;
  79.     const char far*         GetDefaultExt() const;
  80.     void                    SetFileFilter(const char far*);
  81.     void                    SetDescription(const char far*);
  82.     void                    SetDefaultExt(const char far*);
  83.  
  84.  
  85.   protected:                // Called only from parameterized derived class
  86.     TDocTemplate(TRegList& regList, TModule*& module, TDocTemplate*& phead);
  87.    ~TDocTemplate();
  88.  
  89.   private:
  90.     char far* Directory;        // Current directory path, 0 until changed
  91.     long      Flags;            // dtXxx bits for dialogs and creation modes
  92.     TDocManager* DocManager;    // Pointer back to document manager
  93.     int       RefCnt;           // Documents attached + 1 if attached to docmgr
  94.     TModule** ModulePtr;        // Would be TModule*&, except for streaming
  95.     TModule*  ModuleDeref;      // Used only when setting a new module
  96.  
  97. #if defined(OWL2_COMPAT)
  98.   // The following functions are maintained for backward compatability
  99.   // with code generated for versions prior to enhanced document templates
  100.   //
  101.   public:
  102.     virtual TDocument* CreateDoc(const char far* path, long flags = 0) = 0;
  103.     virtual TView*     CreateView(TDocument& doc, long flags = 0) = 0;
  104.     TDocument*         InitDoc(TDocument* doc,const char far* path,long flags);
  105.     TView*             InitView(TView* view);
  106.     bool               SelectSave(TDocument& doc);
  107.  
  108.   protected:                // Called only from parameterized derived class
  109.     TDocTemplate(const char* desc, const char* filt,
  110.                  const char* dir,  const char* ext, long flags,
  111.                  TModule*& module, TDocTemplate*& phead);
  112.   private:
  113.     static bool       _CALLCNVN (*SelectSave_)(TDocTemplate* tpl, TDocument& doc);
  114.     static TView*     _CALLCNVN (*InitView_)(TView* view);
  115.     static TDocument* _CALLCNVN (*InitDoc_)(TDocTemplate& tpl, TDocument* doc,
  116.                                   const char far* path, long flags);
  117. #endif
  118.  
  119.   DECLARE_ABSTRACT_STREAMABLE(_OWLCLASS, TDocTemplate, 1);
  120.   friend class TDocument;   // access to RefCnt
  121.   friend class TDocManager;
  122. };
  123.  
  124. //
  125. // class TDocTemplateT<D,V>
  126. // ~~~~~ ~~~~~~~~~~~~~~~~~~
  127. template<class D, class V>
  128. class _DOCVIEWENTRY TDocTemplateT : public TDocTemplate {
  129.   public:
  130.     TDocTemplateT(TRegList& regList, TModule*& module = ::Module,
  131.                   TDocTemplate*& phead = DocTemplateStaticHead);
  132.     TDocTemplateT(const char* desc, const char* filt,
  133.                   const char* dir, const char* ext, long flags = 0,
  134.                   TModule*& module = ::Module,
  135.                   TDocTemplate*& phead = DocTemplateStaticHead);
  136.     TDocTemplateT* Clone(TModule* module,
  137.                          TDocTemplate*& phead = DocTemplateStaticHead);
  138.  
  139.     D* ConstructDoc(TDocument* parent = 0);
  140.     V* ConstructView(TDocument& doc);
  141.     D* IsMyKindOfDoc(TDocument& doc);  // Returns 0 if template can't support
  142.     V* IsMyKindOfView(TView& view);    // Returns 0 if template incompatible
  143.  
  144.     virtual const char far* GetViewName();
  145.  
  146. #if defined(OWL2_COMPAT)
  147.     // The following functions are maintained for backward compatability
  148.     //
  149.     D*     CreateDoc(const char far* path, long flags = 0);
  150.     TView* CreateView(TDocument& doc, long flags = 0);
  151. #endif
  152.  
  153. #if !defined(BI_NO_OBJ_STREAMING)
  154.   // Explicit expansion of DECLARE_STREAMABLE for use with parameterized class
  155.   // DECLARE_STREAMABLE_FROM_BASE(TDocTemplateT<D,V>);
  156.   //
  157.   #define TEMPLATECLASS TDocTemplateT<D,V>
  158.   DECLARE_CASTABLE;
  159.   DECLARE_STREAMABLE_CTOR(TDocTemplateT);
  160.   DECLARE_STREAMER_FROM_BASE(_DOCVIEWENTRY, TEMPLATECLASS, TDocTemplate);
  161.   DECLARE_STREAMABLE_OPS(TEMPLATECLASS);
  162.   #undef TEMPLATECLASS
  163. #endif
  164. };
  165.  
  166. // Generic definitions/compiler options (eg. alignment) following the 
  167. // definition of classes
  168. #include <services/posclass.h>
  169.  
  170. //----------------------------------------------------------------------------
  171. // Template flags used in reg parameter "docflags"
  172. //
  173.  
  174. //
  175. // Definitions of dtXxx document/view flags for templates and CreateDoc()
  176. //
  177. const long dtNewDoc     = 0x80000000L; // create new document, no path used
  178. const long dtAutoDelete = 0x40000000L; // delete doc when last view is deleted
  179. const long dtNoAutoView = 0x20000000L; // no automatic create of default view
  180. const long dtSingleView = 0x10000000L; // only a single view per document
  181. const long dtAutoOpen   = 0x08000000L; // open document upon creation
  182. const long dtUpdateDir  = 0x04000000L; // syncronize directory with dialog dir
  183. const long dtHidden     = 0x02000000L; // hide template from user selection
  184. const long dtSelected   = 0x01000000L; // indicates template last selected
  185. const long dtDynRegInfo = 0x00800000L; // reginfo table is dynamic, not static
  186. const long dtSingleUse  = 0x00400000L; // to be registered as single use
  187. const long dtRegisterExt= 0x00200000L; // register extension with this app
  188.  
  189. //
  190. // Flags defined by Windows in <commdlg.h> - use the low order 17 bits
  191. //
  192. const long dtReadOnly       = 0x0001; //OFN_READONLY;         // initial check readonly box
  193. const long dtOverwritePrompt= 0x0002; //OFN_OVERWRITEPROMPT;  // prompt before overwrite
  194. const long dtHideReadOnly   = 0x0004; //OFN_HIDEREADONLY;     // no show readonly box
  195. const long dtPathMustExist  = 0x0800; //OFN_PATHMUSTEXIST;    // path must be valid
  196. const long dtFileMustExist  = 0x1000; //OFN_FILEMUSTEXIST;    // file must exist
  197. const long dtCreatePrompt   = 0x2000; //OFN_CREATEPROMPT;     // prompt before create
  198. const long dtNoReadOnly     = 0x8000; //OFN_NOREADONLYRETURN; // return writable file
  199. const long dtNoTestCreate   = 0x10000L;//OFN_NOTESTFILECREATE; // skip create tests;
  200.  
  201. #if defined(BI_NAMESPACE)
  202. } // namespace OWL
  203. #endif
  204.  
  205. //----------------------------------------------------------------------------
  206. // Inline implementations
  207. //
  208.  
  209. //
  210. inline TDocManager* TDocTemplate::GetDocManager() const
  211. {
  212.   return DocManager;
  213. }
  214.  
  215. //
  216. inline void TDocTemplate::SetDocManager(TDocManager* dm)
  217. {
  218.   DocManager = dm;
  219. }
  220.  
  221. //
  222. inline TDocTemplate* TDocTemplate::GetNextTemplate() const
  223. {
  224.   return (TDocTemplate*)Next;
  225. }
  226.  
  227. //
  228. inline bool TDocTemplate::IsFlagSet(long flag) const
  229. {
  230.   return (GetFlags() & flag) != 0;
  231. }
  232.  
  233. //
  234. inline long TDocTemplate::GetFlags() const
  235. {
  236.   return Flags;
  237. }
  238.  
  239. //
  240. inline bool TDocTemplate::IsStatic() const
  241. {
  242.   return (RefCnt & 0x8000) != 0;
  243. }
  244.  
  245. //
  246. inline TModule*& TDocTemplate::GetModule()
  247. {
  248.   return *ModulePtr;
  249. }
  250.  
  251. //
  252. // Return the number of reference count of the template.
  253. // NOTE: The reference count of static templates has the high bit set.
  254. //
  255. inline int TDocTemplate::GetRefCount() const
  256. {
  257.   return RefCnt;
  258. }
  259.  
  260. //
  261. inline void TDocTemplate::SetModule(TModule* module)
  262. {
  263.   ModuleDeref = module;
  264.   ModulePtr = &ModuleDeref;
  265. }
  266.  
  267. //
  268. inline bool
  269. TDocTemplate::IsVisible()
  270. {
  271.   return (GetFlags() & dtHidden) == 0;
  272. }
  273.  
  274. //
  275. template<class D, class V> inline
  276. TDocTemplateT<D,V>::TDocTemplateT(TRegList& regList, TModule*& module,
  277.                                   TDocTemplate*& phead)
  278. :
  279.   TDocTemplate(regList, module, phead)
  280. {
  281. }
  282.  
  283. //
  284. template<class D, class V> inline
  285. TDocTemplateT<D,V>::TDocTemplateT(const char* desc, const char* filt,
  286.                         const char* dir,  const char* ext, long flags,
  287.                         TModule*& module, TDocTemplate*& phead)
  288. :
  289.   TDocTemplate(desc, filt, dir, ext, flags, module, phead)
  290. {
  291. }
  292.  
  293. //
  294. template<class D, class V> inline TDocTemplateT<D,V>*
  295. TDocTemplateT<D,V>::Clone(TModule* module, TDocTemplate*& phead)
  296. {
  297.   TDocTemplateT<D,V>* tpl = new TDocTemplateT<D,V>(GetRegList(),
  298.                                                    GetModule(), phead);
  299.   tpl->SetModule(module);
  300.   return tpl;
  301. }
  302.  
  303. #if !defined(BI_COMP_MSC)  
  304. //
  305. // 'Factory' method to create a new document of type 'D' using the specified
  306. // parameter as the parent document.
  307. //
  308. template<class D, class V> inline D*
  309. TDocTemplateT<D,V>::ConstructDoc(TDocument* parent = 0)
  310. {
  311.   return new D(parent);
  312. }
  313. #endif
  314.  
  315. //
  316. // 'Factory' method to create a new view of type 'V' from the specified
  317. // document parameter.
  318. //
  319. template<class D, class V> inline V*
  320. TDocTemplateT<D,V>::ConstructView(TDocument& doc)
  321. {
  322.   return new V((D&)doc);
  323. }
  324.  
  325. //
  326. template<class D, class V> inline const char far*
  327. TDocTemplateT<D,V>::GetViewName()
  328. {
  329.   return V::StaticName();
  330. }
  331.  
  332. //----------------------------------------------------------------------------
  333.  
  334. #if defined(BI_NO_NEW_CASTS)  // cannot use templates with TYPESEAFE_DOWNCAST
  335.  
  336. #define DEFINE_DOC_TEMPLATE_CLASS(docClass, viewClass, tplClass) \
  337.   typedef TDocTemplateT<docClass, viewClass> tplClass;           \
  338.   IMPLEMENT_STREAMABLE_FROM_BASE(tplClass, TDocTemplate);        \
  339.   docClass* tplClass::IsMyKindOfDoc(TDocument& doc)              \
  340.   {                                                              \
  341.     return TYPESAFE_DOWNCAST(&doc, docClass);                    \
  342.   }                                                              \
  343.   viewClass* tplClass::IsMyKindOfView(TView& view)               \
  344.   {                                                              \
  345.     return TYPESAFE_DOWNCAST(&view, viewClass);                  \
  346.   }
  347.  
  348. #else                         // with RTTI, all functions can be templatized
  349.  
  350. template<class D, class V> D*
  351. TDocTemplateT<D,V>::IsMyKindOfDoc(TDocument& doc)
  352. {
  353.   return dynamic_cast<D*>(&doc);
  354. }
  355.  
  356. template<class D, class V> V*
  357. TDocTemplateT<D,V>::IsMyKindOfView(TView& view)
  358. {
  359.   return dynamic_cast<V*>(&view);
  360. }
  361.  
  362. #define DEFINE_DOC_TEMPLATE_CLASS(docClass, viewClass, tplClass) \
  363.   typedef TDocTemplateT<docClass, viewClass> tplClass;           \
  364.   IMPLEMENT_STREAMABLE_FROM_BASE(tplClass, TDocTemplate);
  365.  
  366. #endif
  367.  
  368. //----------------------------------------------------------------------------
  369. #if defined(OWL2_COMPAT)
  370.  
  371. //
  372. inline TDocument*
  373. TDocTemplate::InitDoc(TDocument* doc, const char far* path, long flags)
  374. {
  375.   return InitDoc_(*this, doc, path, flags);
  376. }
  377.  
  378. //
  379. inline TView*
  380. TDocTemplate::InitView(TView* view)
  381. {
  382.   return InitView_(view);
  383. }
  384.  
  385. //
  386. inline bool
  387. TDocTemplate::SelectSave(TDocument& doc)
  388. {
  389.   return SelectSave_(this, doc);
  390. }
  391.  
  392. //
  393. template<class D, class V> inline D*
  394. TDocTemplateT<D,V>::CreateDoc(const char far* path, long flags)
  395. {
  396.   return (D*)InitDoc(new D((TDocument*)0), path, flags);
  397. }
  398.  
  399. //
  400. template<class D, class V> inline TView*
  401. TDocTemplateT<D,V>::CreateView(TDocument& doc, long /*flags*/)
  402. {
  403.   return (V*)InitView(new V((D&)doc));
  404. }
  405. #endif  // defined(OWL2_COMPAT)
  406.  
  407. #endif  // OWL_DOCTPL_H
  408.